//
//  UIImage+SLCreateGradientImage.m
//  shunlianPower2
//
//  Created by 陈镇 on 2020/6/28.
//  Copyright © 2020 顺联网络. All rights reserved.
//

#import "UIImage+SLCreateGradientImage.h"

@implementation UIImage (SLCreateGradientImage)

+(UIImage *)sl_createHorizontalImageSize:(CGSize)size startColor:(UIColor *)startColor endColor:(UIColor *)endColor cornerRadius:(CGFloat)cornerRadius{
    
    return [self sl_createImageSize:size startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0) startColor:startColor endColor:endColor cornerRadius:cornerRadius];
    
    
}
+(UIImage *)sl_createVerticalImageSize:(CGSize)size startColor:(UIColor *)startColor endColor:(UIColor *)endColor cornerRadius:(CGFloat)cornerRadius{
    
    return [self sl_createImageSize:size startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1) startColor:startColor endColor:endColor cornerRadius:cornerRadius];
    
    
}




+(UIImage *)sl_createImageSize:(CGSize)size startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint startColor:(UIColor *)startColor endColor:(UIColor *)endColor cornerRadius:(CGFloat)cornerRadius{
    
    
    if (size.width==0 || size.height == 0) {
        return nil;
    }
    
    CGFloat scale = [UIScreen mainScreen].scale;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width * scale, size.height * scale)];
    view.layer.cornerRadius = cornerRadius * scale;
    view.layer.masksToBounds = YES;
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, view.width, view.height);
    gl.startPoint = startPoint;
    gl.endPoint = endPoint;
    gl.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
    gl.locations = @[@(0), @(1.0f)];
    [view.layer addSublayer:gl];
    
    UIImage *tmp = [UIImage qmui_imageWithView:view];
    return tmp;
}

- (UIImage *)sl_imageResizedInLimitedToWidth:(CGFloat)width{
    return [self qmui_imageResizedInLimitedSize:CGSizeMake(width , (self.size.height/self.size.width * width))];
}
- (UIImage *)sl_imageResizedInLimitedToScreenWidth{
    return [self sl_imageResizedInLimitedToWidth:SCREEN_WIDTH];
}

-(UIImage *)sl_resizableImageWithCapInsets:(UIEdgeInsets)edgeInsets{

    // 设置端盖的值
    CGFloat top = self.size.height * edgeInsets.top;
    CGFloat left = self.size.width * edgeInsets.left;
    CGFloat bottom = self.size.height * edgeInsets.bottom;
    CGFloat right = self.size.width * edgeInsets.right;
    
    
    if (top > 1) {
        top = top - 1;
    }
    if (left > 1) {
        left = left - 1;
    }
    if (bottom > 1) {
        bottom = bottom - 1;
    }
    if (right > 1) {
        right = right - 1;
    }
    
    // 设置端盖的值
    UIEdgeInsets edgeInsets2 = UIEdgeInsetsMake(top, left, bottom, right);
    // 设置拉伸的模式
    UIImageResizingMode mode = UIImageResizingModeStretch;
    // 拉伸图片
    return [self resizableImageWithCapInsets:edgeInsets2 resizingMode:mode];
}
-(UIImage *)sl_resizedInLimitedToScreenWidthAndResizableImageWithCapInsets:(UIEdgeInsets)edgeInsets{
    
    UIImage *tmp = [self qmui_imageResizedInLimitedSize:CGSizeMake(SCREEN_WIDTH , (self.size.height/self.size.width * SCREEN_WIDTH))];
    return [tmp sl_resizableImageWithCapInsets:edgeInsets];
}
@end
